<?php

class MasterController extends BaseController {

	//ADD Features
	public function getAddfeatures()
	{
		Session::forget('list');
		Session::forget('filter');
		Session::forget('filterby');
		Session::forget('name');
		$addfeatures = DB::table('ref_additional_features')
							->where('additional_features_isdelete','=',0)
							->paginate(10);
		return View::make('admin.master.addfeatures')->with('addfeatures',$addfeatures);
	}

	public function postAddfeatures()
	{
			
		if(Input::get('filter')=='type'){
			Session::forget('name');
			Session::put('filter','type');
			Session::put('list', Input::get('typeby'));
			if(Input::get('typeby')=='all'){
				$addfeatures = DB::table('ref_additional_features')
					->where('additional_features_isdelete','=',0)
					->paginate(10);
			}else{
				$addfeatures = DB::table('ref_additional_features')
					->where('additional_features_isdelete','=',0)
					->where('additional_features_type','=',Input::get('typeby'))
					->paginate(10);
			}
		}else if(Input::get('filter')=='keyword'){
			Session::forget('list');
			Session::put('filter','keyword');
			Session::put('name',Input::get('name'));
			$addfeatures = DB::table('ref_additional_features')
				->where('additional_features_isdelete','=',0)
				->where('additional_features_name','LIKE','%'.Input::get('name').'%')
				->paginate(10);	
		}else{
			if(Session::get('filter')=="type"){
			if(Session::get('list')=='all'){
				$addfeatures = DB::table('ref_additional_features')
					->where('additional_features_isdelete','=',0)
					->paginate(10);
			}else{
				$addfeatures = DB::table('ref_additional_features')
					->where('additional_features_isdelete','=',0)
					->where('additional_features_type','=',Session::get('list'))
					->paginate(10);
			}
			}else if(Session::get('filter')=="keyword"){
				$addfeatures = DB::table('ref_additional_features')
					->where('additional_features_isdelete','=',0)
					->where('additional_features_name','LIKE','%'.Session::get('name').'%')
					->paginate(10);	
			}
		}
		return View::make('admin.master.addfeatures')->with('addfeatures',$addfeatures);
	}

	public function postAddaddfeatures()
	{
		$addaddfeatures = refadditionalfeatures::create(Input::all());

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Add data fitur tambahan";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/master/addfeatures')->with('message','Data fitur tambahan berhasil ditambahkan');
	}

	public function postEditaddfeatures($id)
	{
		$editaddfeatures = refadditionalfeatures::find($id);
		$editaddfeatures->additional_features_type = Input::get('additional_features_type');
		$editaddfeatures->additional_features_name = Input::get('additional_features_name');
		$editaddfeatures->additional_features_price = Input::get('additional_features_price');
		$editaddfeatures->additional_features_unit = Input::get('additional_features_unit');
		$editaddfeatures->save();

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Edit data fitur tambahan ".$id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/master/addfeatures')->with('message','Data fitur tambahan berhasil diubahn');
	}

	public function postDeleteaddfeatures()
	{
		$addfeatures = DB::table('ref_additional_features')
				->get();
		foreach($addfeatures as $addfeatures){
		if(Input::get($addfeatures->additional_features_id)=='1'){
			$id = Input::get($addfeatures->additional_features_id);
			$addfeat = DB::table('ref_additional_features')
					->where('additional_features_id','=',$addfeatures->additional_features_id)
					->update(array('additional_features_isdelete' => 1));

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Hapus data fitur tambahan ".$addfeatures->additional_features_id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		}
		}
		return Redirect::to('/master/addfeatures')->with('message','Data fitur tambahan berhasil dihapus');
	}




	//paper
	//ADD Features
	public function getPapers()
	{
		Session::forget('list');
		Session::forget('filter');
		Session::forget('filterby');
		Session::forget('name');
		$papers = DB::table('ref_papertype')
							->where('papertype_isdelete','=',0)
							->paginate(10);
		return View::make('admin.master.papers')->with('papers',$papers);
	}

	public function postPapers()
	{
		if(Input::get('filter')=='type'){
			Session::forget('name');
			Session::put('filter','type');
			Session::put('list', Input::get('typeby'));
				if(Input::get('typeby')=='all'){
					$papers = DB::table('ref_papertype')
						->where('papertype_isdelete','=',0)
						->paginate(10);
				}else{
					$papers = DB::table('ref_papertype')
						->where('papertype_isdelete','=',0)
						->where('papertype_type','=',Input::get('typeby'))
						->paginate(10);
				}
		}else if(Input::get('filter')=='keyword'){
				Session::forget('list');
				Session::put('filter','keyword');
				Session::put('name',Input::get('name'));
				$papers = DB::table('ref_papertype')
					->where('papertype_isdelete','=',0)
					->where('papertype_name','LIKE','%'.Input::get('name').'%')
					->paginate(10);
		}else{
			if(Session::get('filter')=="type"){
					if(Input::get('typeby')=='all'){
					$papers = DB::table('ref_papertype')
							->where('papertype_isdelete','=',0)
							->paginate(10);
					}else{
						$papers = DB::table('ref_papertype')
							->where('papertype_isdelete','=',0)
							->where('papertype_type','=',Input::get('typeby'))
							->paginate(10);
					}
			}else if(Session::get('filter')=="keyword"){
					$papers = DB::table('ref_papertype')
					->where('papertype_isdelete','=',0)
					->where('papertype_name','LIKE','%'.Input::get('name').'%')
					->paginate(10);
			}
		}



		return View::make('admin.master.papers')->with('papers',$papers);
	}

	public function postAddpapers()
	{
		$addpapers = refpapertypes::create(Input::all());

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Add data kertas";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/master/papers')->with('message','Data kertas berhasil ditambahkan');
	}

	public function postEditpapers($id)
	{
		$editpapers = refpapertypes::find($id);
		$editpapers->papertype_type = Input::get('papertype_type');
		$editpapers->papertype_name = Input::get('papertype_name');
		$editpapers->save();

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Edit data kertas ".$id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/master/papers')->with('message','Data kertas berhasil diubahn');
	}

	public function postDeletepapers()
	{
		$papers = DB::table('ref_papertype')
				->get();
		foreach($papers as $paper){
		if(Input::get($paper->papertype_id)=='1'){
			$id = Input::get($paper->papertype_id);
			$papers = DB::table('ref_papertype')
					->where('papertype_id','=',$paper->papertype_id)
					->update(array('papertype_isdelete' => 1));

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Delete data kertas ".$paper->papertype_id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		}
		}
		return Redirect::to('/master/papers')->with('message','Data kertas berhasil dihapus');
	}

	public function getAgenbonus()
	{
		$databonus = refbonus::all();
		return View::make('admin.master.bonus',compact('databonus',$databonus));
	}


	public function postAgenbonus($id)
	{
		$databonus = refbonus::find($id);
		$databonus->bonus_config_percentage = Input::get('bonus');
		$databonus->save();

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Edit bonus";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/bonus')->with('message','Bonus berhasil diganti');
	}


}